package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.util.CollectionUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCbrList(Long catId) {
        List<CategoryBrandRelationEntity> catelogList = this.baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", catId));
        return catelogList;
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateBrand(BrandEntity brand) {

        QueryWrapper<CategoryBrandRelationEntity> wraper = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brand.getBrandId());
        CategoryBrandRelationEntity crt = new CategoryBrandRelationEntity();
        crt.setBrandId(brand.getBrandId());
        crt.setBrandName(brand.getName());
        this.baseMapper.update(crt, wraper);
    }

    @Override
    public List<BrandVo> getBrandsByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationDao.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(categoryBrandRelationEntities)) {
            return Collections.EMPTY_LIST;
        }


        List<BrandEntity> collect1 = categoryBrandRelationEntities.stream().map(x -> {
            Long brandId = x.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());


        List<BrandVo> collect = collect1.stream().map(x -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(x.getBrandId());
            brandVo.setBrandName(x.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return collect;
    }

}
